#include "stm32f4xx.h"                  // Device header
#include "DELAY/delay.h"
#include "KEY/Key.h"
void Key_Init(void)
{
	RCC_AHB1PeriphClockCmd(RCC_KEY1_GPIO | RCC_KEY2_GPIO | RCC_KEY3_GPIO | RCC_KEY4_GPIO,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;	// 上拉
	GPIO_InitStructure.GPIO_Pin = KEY1_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   //50MHz是端口的输出速度，对按键没多大影响
	GPIO_Init(KEY1_GPIO,&GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = KEY2_Pin;
	GPIO_Init(KEY2_GPIO,&GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = KEY3_Pin;
	GPIO_Init(KEY3_GPIO,&GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = KEY3_Pin;
	GPIO_Init(KEY3_GPIO,&GPIO_InitStructure);
}


uint8_t  Key_Scan(void)
{
	 uint8_t keyval = 0;
	static uint8_t state = 0;
    if ((state == 0) && (KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0))  /* 有任意一个按键按下了 */
    {
        delay_ms(10);           /* 去抖动 */
		state = 1;
        if (KEY1 == 0)  keyval = LEFT;

        if (KEY2 == 0)  keyval = DOWN;

        if (KEY3 == 0)  keyval = RIGHT;

        if (KEY4 == 0) 	keyval = UP;
    }
	else if((KEY1 == 1) && (KEY2 == 1) && (KEY3 == 1) && (KEY4 == 1))
	{
		state = 0;
	}
    return keyval;              /* 返回键值 */
}
